package cn.lunkr.sdkdemo.ui.table.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentResultListener;
import androidx.fragment.app.FragmentTransaction;

import cn.lunkr.base.ui.BasicFragment;
import cn.lunkr.mailui.ui.Contact1sBarFragment;
import cn.lunkr.mailui.ui.CustomPopWindow;
import cn.lunkr.mailui.ui.OabBarFragment;
import cn.lunkr.mailui.ui.PabBarFragment;
import cn.lunkr.sdkdemo.R;
import cn.lunkr.sdkdemo.databinding.FragmentTableContactBinding;
import cn.lunkr.sdkdemo.ui.table.activity.TableSearchActivity;

public class TableContactFragment extends BasicFragment implements FragmentResultListener, Contact1sBarFragment.Contact1BarFragmentListener {

    private FragmentTableContactBinding viewBinding;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        viewBinding = FragmentTableContactBinding.inflate(getLayoutInflater());
        initFragAction();
        return viewBinding.getRoot();
    }

    private void initFragAction() {
        requireActivity().getSupportFragmentManager().setFragmentResultListener("click_pab", this, this);
        requireActivity().getSupportFragmentManager().setFragmentResultListener("click_oab", this, this);
    }

    @Override
    public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        switch (requestKey) {
            case "click_oab":
                OabBarFragment fragment = new OabBarFragment();
                fragment.setArguments(result);
                ft.replace(R.id.detail_container, fragment);
                break;
            case "click_pab":
                ft.replace(R.id.detail_container, new PabBarFragment());
                break;
        }
        if (viewBinding.slidingPaneLayout.isOpen()) {
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        }
        ft.commit();
        viewBinding.slidingPaneLayout.open();
    }

    @Override
    public boolean onContactsSearchClick(CustomPopWindow.PopItem popItem) {
        startActivity(new Intent(getActivity(), TableSearchActivity.class));
        return true;
    }
}